public class Test {


    public static void main(String[] args) throws InterruptedException {

        Counter counter = new Counter();
        Thread t1 = new Thread(()-> {
           for (int i = 0;i < 50000 ;i++) {
                counter.add();
           }
        });

        Thread t2 = new Thread(()-> {
            for (int i = 0; i < 50000;i++) {
                counter.add();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(counter.get());
    }
}

class Counter {
    public  int count;
    private Object locker = new Object();
    public void add2() {
        synchronized (locker) {
            count++;
        }

    }

    public void add() {
        synchronized (this) {
            count++;
        }

    }

    synchronized public void add1() {
         {
            count++;
        }

    }

    synchronized public static void test() {

    }

    public static void test2() {
        synchronized (Counter.class) {

        }
    }

    public int get() {
        return count;
    }
}
